# 使用多阶段构建优化镜像大小
FROM node:18-alpine AS base
WORKDIR /app

# ----------------------------------------
# 依赖安装阶段（关键修复区域）
# ----------------------------------------
FROM base AS deps
# 配置清华源（仅替换域名，保留版本路径）
RUN sed -i 's|https://dl-cdn.alpinelinux.org|https://mirrors.tuna.tsinghua.edu.cn|g' /etc/apk/repositories \
    && apk update \
    && apk add --no-cache gcompat
# 复制包管理文件
COPY package.json yarn.lock* ./
# 配置 yarn 国内镜像源并安装依赖
RUN yarn config set registry 'https://registry.npmmirror.com/' \
    && yarn install --frozen-lockfile

# ----------------------------------------
# 构建阶段
# ----------------------------------------
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN yarn build

# ----------------------------------------
# 运行阶段
# ----------------------------------------
FROM base AS runner
WORKDIR /app
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/.next/server ./.next/server

EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME "0.0.0.0"
CMD ["node", "server.js"]